# -*- coding: utf-8 -*-
# @Author   : zxc19
# @Time     : 2023/7/11 17:55
# @FileName : comparators.py
# @Desc     : 请输入功能模块描述
import json
import re


class Comparator(object):
    @staticmethod
    def eq(actual_value, expect_value):
        """
        实际值与期望值相等
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert actual_value == expect_value

    @staticmethod
    def lt(actual_value, expect_value):
        """
        实际值小于期望值
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert actual_value < expect_value

    @staticmethod
    def lte(actual_value, expect_value):
        """
        实际值小于或等于期望值
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert actual_value <= expect_value

    @staticmethod
    def gt(actual_value, expect_value):
        """
        实际值大于期望值
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert actual_value > expect_value

    @staticmethod
    def gte(actual_value, expect_value):
        """
        实际值大于或等于期望值
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert actual_value >= expect_value

    @staticmethod
    def neq(actual_value, expect_value):
        """
        实际值与期望值不相等
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert actual_value != expect_value

    @staticmethod
    def str_eq(actual_value, expect_value):
        """
        字符串实际值与期望值相同
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert str(actual_value) == str(expect_value)

    @staticmethod
    def length_eq(actual_value, expect_value):
        """
        实际值的长度等于期望长度
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(expect_value, (int,))
        assert len(actual_value) == expect_value

    @staticmethod
    def length_gt(actual_value, expect_value):
        """
        实际值的长度大于期望长度
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(expect_value, (int,))
        assert len(actual_value) > expect_value

    @staticmethod
    def length_gte(actual_value, expect_value):
        """
        实际值的长度大于或等于期望长度
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(expect_value, (int,))
        assert len(actual_value) >= expect_value

    @staticmethod
    def length_lt(actual_value, expect_value):
        """
        实际值的长度小于期望长度
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(expect_value, (int,))
        assert len(actual_value) < expect_value

    @staticmethod
    def length_lte(actual_value, expect_value):
        """
        实际值的长度小于或等于期望长度
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(expect_value, (int,))
        assert len(actual_value) <= expect_value

    @staticmethod
    def contains(actual_value, expect_value):
        """
        期望值包含在实际值中
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(actual_value, (list, tuple, dict, str, bytes))
        assert expect_value in actual_value

    @staticmethod
    def contained_by(actual_value, expect_value):
        """
        实际值被包含在期望值中
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert isinstance(expect_value, (list, tuple, dict, str, bytes))
        assert actual_value in expect_value

    @staticmethod
    def type_match(actual_value, expect_value):
        """
        实际值的类型与期望值的类型相匹配
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """

        def get_type(name):
            if isinstance(name, type):
                return name
            elif isinstance(name, (str, bytes)):
                try:
                    return __builtins__[name]
                except KeyError:
                    raise ValueError(name)
            else:
                raise ValueError(name)

        assert isinstance(actual_value, get_type(expect_value))

    @staticmethod
    def regex_match(actual_value, expect_value):
        """
        正则匹配(从字符串的起始位置匹配)
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        if not isinstance(actual_value, str):
            actual_value = json.dumps(actual_value, ensure_ascii=False)
        if not isinstance(expect_value, str):
            expect_value = json.dumps(expect_value, ensure_ascii=False)
        assert re.match(expect_value, actual_value)

    @staticmethod
    def regex_search(actual_value, expect_value):
        """
        正则匹配(从字符串的任意位置匹配)
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        if not isinstance(actual_value, str):
            actual_value = json.dumps(actual_value, ensure_ascii=False)
        if not isinstance(expect_value, str):
            expect_value = json.dumps(expect_value, ensure_ascii=False)
        assert re.search(expect_value, actual_value)

    @staticmethod
    def startswith(actual_value, expect_value):
        """
        实际值是以期望值开始
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert str(actual_value).startswith(str(expect_value))

    @staticmethod
    def endswith(actual_value, expect_value):
        """
        实际值是以期望值结束
        :param actual_value:实际值
        :param expect_value:期望值
        :return:
        """
        assert str(actual_value).endswith(str(expect_value))
